package com.lxk.mall.pms.controller.admin.spu;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lxk.mall.pms.convert.spu.ProductSpuConvert;
import com.lxk.mall.pms.domain.PmsProductSku;
import com.lxk.mall.pms.domain.PmsProductSpu;
import com.lxk.mall.pms.domain.bo.spu.ProductSpuBatchUpdateStatusReqBO;
import com.lxk.mall.pms.domain.bo.spu.ProductSpuPageReqBO;
import com.lxk.mall.pms.domain.bo.spu.ProductSpuSaveReqBO;
import com.lxk.mall.pms.domain.bo.spu.ProductSpuUpdateStatusReqBO;
import com.lxk.mall.pms.domain.vo.spu.PmsProductSpuVo;
import com.lxk.mall.pms.domain.vo.spu.ProductSpuRespVO;
import com.lxk.mall.pms.domain.vo.spu.ProductSpuSimpleRespVO;
import com.lxk.mall.pms.enums.ProductSpuStatusEnum;
import com.lxk.mall.pms.service.IPmsProductSkuService;
import com.lxk.mall.pms.service.IPmsProductSpuService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品spu
 *
 * @author Lion Li
 * @date 2024-06-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pms/spu")
public class PmsProductSpuController extends BaseController {

    private final IPmsProductSpuService pmsProductSpuService;
    private final IPmsProductSkuService pmsProductSkuService;

    /**
     * 查询商品spu列表
     */
    @SaCheckPermission("pms:spu:list")
    @GetMapping("/list")
    public TableDataInfo<PmsProductSpuVo> list(ProductSpuPageReqBO bo, PageQuery pageQuery) {
        return pmsProductSpuService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品spu列表
     */
    @SaCheckPermission("pms:spu:export")
    @Log(title = "商品spu", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductSpuPageReqBO bo, HttpServletResponse response) {
        List<PmsProductSpuVo> list = pmsProductSpuService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品spu", PmsProductSpuVo.class, response);
    }

    /**
     * 获取商品spu详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pms:spu:query")
    @GetMapping("/{id}")
    public R<ProductSpuRespVO> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        PmsProductSpu spu = pmsProductSpuService.queryById(id);
        if (spu == null) {
            return R.ok();
        }
        // 查询商品 SKU
        List<PmsProductSku> skuList = pmsProductSkuService.getSkuListBySpuId(spu.getId());
        return R.ok(ProductSpuConvert.INSTANCE.buildSpuRespVO(spu, skuList));
    }

    /**
     * 新增商品spu
     */
    @SaCheckPermission("pms:spu:add")
    @Log(title = "商品spu", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody ProductSpuSaveReqBO createReqVO) {
        return R.ok(pmsProductSpuService.createSpu(createReqVO));
    }

    /**
     * 修改商品spu
     */
    @SaCheckPermission("pms:spu:edit")
    @Log(title = "商品spu", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductSpuSaveReqBO updateReqVO) {
        pmsProductSpuService.updateSpu(updateReqVO);
        return R.ok();
    }

    /**
     * 删除商品spu
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pms:spu:remove")
    @Log(title = "商品spu", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(pmsProductSpuService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获得商品 SPU 分页 tab count
     *
     * @return
     */
    @GetMapping("/getCount")
    @SaCheckPermission("pms:spu:list")
    public R<Map<Integer, Long>> getSpuCount(String productType) {
        return R.ok(pmsProductSpuService.getTabsCount(productType));
    }

    /**
     * 获得商品 SPU 精简列表
     *
     * @return
     */
    @GetMapping("/listAllSimple")
    @SaCheckPermission("pms:spu:list")
    public R<List<ProductSpuSimpleRespVO>> getSpuSimpleList() {
        List<PmsProductSpu> list = pmsProductSpuService.getSpuListByStatus(ProductSpuStatusEnum.ENABLE.getStatus());
        // 降序排序后，返回给前端
        List<ProductSpuSimpleRespVO> vos = list.stream().map(e -> {
            ProductSpuSimpleRespVO respVO = new ProductSpuSimpleRespVO();
            BeanUtils.copyProperties(e, respVO);
            return respVO;
        }).collect(Collectors.toList());
        return R.ok(vos);
    }

    @PutMapping("/updateStatus")
    @Operation(summary = "更新商品 SPU Status")
    @SaCheckPermission("pms:spu:updateStatus")
    public R<Boolean> updateStatus(@Valid @RequestBody ProductSpuUpdateStatusReqBO updateReqVO) {
        pmsProductSpuService.updateSpuStatus(updateReqVO);
        return R.ok(true);
    }

    @PutMapping("/batch/updateStatus")
    @Operation(summary = "更新商品 SPU Status")
    @SaCheckPermission("pms:spu:updateStatus")
    public R<Boolean> updateStatus(@Valid @RequestBody ProductSpuBatchUpdateStatusReqBO updateReqVO) {
        pmsProductSpuService.updateBatchSpuStatus(updateReqVO);
        return R.ok(true);
    }
}
